<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('usersmodel');
		$this->load->helper('flexigrid');
	}

	public function index()
	{
		//ver lib

		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */


		$colModel['uuid'] = array('UUID',40,TRUE,'center',1,0,'rowClick');
		$colModel['vendorcode'] = array('Code',60,TRUE,'center',2,0,'rowClick');
		$colModel['vfirstname'] = array('ชื่อ',70,TRUE,'left',1,0,'rowClick');
		$colModel['vlastname'] = array('นามสกุล',120,TRUE,'left',1,0,'rowClick');
		$colModel['villagename'] = array('กลุ่ม',130, TRUE,'left',0,0,'rowClick');
		$colModel['vmobile'] = array('เบอร์โทรศัพท์',80, TRUE, 'right',0,0,'rowClick');
		$colModel['action'] = array('Actions',80, FALSE, 'right',0,0,'rowClick');


		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => 400,
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'ตาราง USER',
		'showTableToggleBtn' => false,
		'showToggleBtn' => false, 
		);

		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		//$buttons[] = array('Delete','delete','test');
		//		$buttons[] = array('separator');
		//		$buttons[] = array('Select All','add','test');
		//		$buttons[] = array('DeSelect All','delete','test');
		//		$buttons[] = array('separator');


		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url('ajax/ajax_users'),$colModel,'uuid','asc',$gridParams,'');

		$data['js_grid'] = $grid_js;

		$this->load->view('users/flexigrid',$data);
	}

	public function form($uuid=0){

		$this->load->model('optionmodel');



		if ($uuid == 0){
			
			$data['uuid'] = 0;
			$data['type_option'] = $this->optionmodel->getVendorTypeOption();
			$data['village_option'] = $this->optionmodel->getVillageOption();
			
			$this->load->view('users/form',$data);
		}else{
			//edit
			$data = $this->usersmodel->get_user($uuid);
			$data['type_option'] = $this->optionmodel->getVendorTypeOption();
			$data['village_option'] = $this->optionmodel->getVillageOption();
			//print_r($data);
			$this->load->view('users/form',$data);
		}

	}

	public function saveForm(){
		$uuid = $this->input->post('uuid');
		if($uuid == 0){
			$insert_data = $this->input->post();
			//print_r($insert_data);
			$lastid = $this->usersmodel->insert_main($insert_data);
		}
		else{
			$update_data = $this->input->post();
			$this->usersmodel->update_main($update_data);
			redirect('users/form/'.$update_data['uuid']);
		}
		redirect('users/index');

	}
	
	public function detail($uuid){
		$data['detail'] = $this->usersmodel->get_user($uuid);
		$data['truckdetail'] = $this->usersmodel->get_user_truck_summary($data['detail']['vendorcode']);
		$this->load->view('users/detail',$data);
	}
	
	public function groupmember(){
		$searchstr = $this->input->post('searchstr');
		if ( ! $searchstr)$searchstr = '';
		$data['groupmember'] = $this->usersmodel->get_group_summary($searchstr);
		$this->load->view('users/groupmember',$data);
	}
	
	public function grouppayment(){
		$this->load->model('optionmodel');
		$data['exist_village_option'] = $this->optionmodel->getExistVillageOption();
		$this->load->view('users/grouppayment',$data);
	}
	
	public function savegrouppayment(){
		$village = $this->input->post('village');
		$money = $this->input->post('money');
		$whopay = $this->input->post('whopay');
		$whoreceive = $this->input->post('whoreceive');
		
		if($this->usersmodel->save_group_payment($village,$money,$whopay,$whoreceive)){
			redirect('users/grouppayment');
		}else{
			echo 'error';
		}
	}
	
	public function grouppaymentdetail($paid_id){
		$paymentdetail = $this->usersmodel->getGroupPaymentDetail($paid_id);
		print_r($paymentdetail);
	}
	
	public function groupdetail($villageid){
		
		$data['groupdetail'] = $this->usersmodel->get_groupdetail($villageid);
		$data['groupdetail_member'] = $this->usersmodel->get_groupdetail_member($villageid);
		$data['member_count'] = count($data['groupdetail_member']);
		
		$this->load->view('users/groupdetail',$data);
	}
	
	public function groupdetailtrans($villageid){

		$data['groupdetailtrans'] = $this->usersmodel->get_groupdetail_transaction($villageid);
		$this->load->view('users/groupdetailtrans',$data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */